# encoding: utf-8
import os
import time
import commands
import httplib

project_path = "/opt/gfzq/web/gfw/server"
log_path = project_path + "/logs"
PORT = 4000


log = file(log_path + "/protect.log" ,"a+")

def restart():
  dt = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
  log.write("[" + dt + "]  开始重启pm2进程\n")
  app_path = project_path + "/app.js"
  os.system("pm2 kill")
  os.system("pm2 start " + app_path + " -i max")
  log.write("启动pm2进程完成\n")

conn = None
try:
  conn = httplib.HTTPConnection("127.0.0.1",PORT,timeout=10)
  conn.request("GET","/ad/list")
  r1 = conn.getresponse()
  if (r1.status != 200):
    restart() 
except Exception,e:
  restart()
finally:
  if conn:
    conn.close()
  log.close()

